﻿var Player;
var widthslot = null;
var widthskid = null;
//widthslot-=widthskid;
var media_url;
var dot = 0;
var basepoint = 0;
var keyframe_time = new Array();
var keyframe_num = new Array();
var totaltime = 0;
var bufftime = 0;
var packagesize = -1;
var headersize = 0;
var time_obj = {};
var pver;
var is_vista = ((navigator.userAgent.indexOf('NT 6.0') > 0) && (navigator.userAgent.indexOf('CLR 3.0.04506') > 0));

var Timer = null; //进度时间handle
var mPlayerVod = {//点播mediaPlayer播放对象
	Delete:function(){
		try
		{
			if(this.timerRunning)
			{
				clearInterval(this.m_time);
				this.timerRunning = false;
			}
			this.mdivStop.onclick();
			this.player=null;
		}catch(err){}
	},
	Init:function(url,pwidth,pheight){
		if (typeof(curPlayer) != "undefined"){
		curPlayer.Delete();}
		this.playerType="temp.wmv";
		this.isvod = true;
		this.isplay = true;
		this.mdivScreen = document.getElementById("ready");
		this.mdivReady = document.getElementById("stopdiv");
		this.mdivAd = document.getElementById("playing");
		this.mdivTime = document.getElementById("percent");
		this.mdivPlay =document.getElementById("play");
		this.mdivStop = document.getElementById("stop");
		this.mdivInfo = document.getElementById("msg");
		this.mdivSound = document.getElementById("mute");
		this.mdivFullScreen = document.getElementById("fullScreen");
		this.stateStr = ["高清视频技术演示","高清视频技术演示","播放暂停","正在播放《"+channelname+"》","向前搜索","向后搜索","正在缓冲","正在等待流开始","播放流已结束","准备新的媒体文件","播放准备就绪","尝试重新连接流媒体数据"];
		this.m_time=null;
		this.timerRunning = false;

//		url = 'http://192.168.1.47:10001/2B7651EFAF46BD72D9C6B0821468823C54F75C92C2EE4297619634BB6BD77860DA27CC4C325A0A88437861C8E8F80A960EAFEEB5DC4C73666A3D3BE34B674A4DB58CAB6E78587DFFF93F056127AD39A3A98635D06E36D6CDB1851779DF7271634750F6F4B053DF0C25BF1EF4814CD938F5477E03F2A58C6649B3FF52834064A28A3D83580BB6638D81FEFB4054E203693AA6A0771D1682088911240E1010DA371E4730642284DEB26492B29203BECB916EB750C70102D35D67B2707156AFCB5AA455F2DFCB111078D5C65575522EAECA7C5A877D3D4E06FC54917C0D410B9351548906EC2UOMUXTDDNVEWPUOR/test-file-10-7.wmv';
		media_url = url;
		var str = '';

		if (!Prototype.Browser.IE)
		{
			str = '<embed id="player" width="'+pwidth+'" height="'+pheight+'" src="'+url+'" type="application/x-mplayer2" showstatusbar=false showcontrols=false showaudiocontrols=false showtracker=false showpositioncontrols=false></embed>';
			this.mdivScreen.innerHTML = str;
			Player =  document.getElementById("player");
			this.player =  Player;
			pver = 11;
		}
		else
		{
			str += '<object ID="player" width="'+pwidth+'" height="'+pheight+'" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" VIEWASTEXT>';
			str += '    <param name="AutoStart" value="false">';
			str += '    <param NAME="Balance" VALUE="1">';
			str += '    <param name="enabled" value="1">';
			str += '    <param name="EnableContextMenu" value="true">';
			str += '    <param name="url" value="">';
			str += '    <param name="PlayCount" value="1">';
			str += '    <param name="rate" value="1">';
			str += '    <param name="currentPosition" value="0">';
			str += '    <param name="currentMarker" value="1">';
			str += '    <param name="defaultFrame" value="">';
			str += '    <param name="invokeURLs" value="0">';
			str += '    <param name="baseURL" value="">';
			str += '    <param name="stretchToFit" value="1">';
			str += '    <param name="volume" value="90">';
			str += '    <param name="mute" value="0">';
			str += '    <param name="uiMode" value="None">';
			str += '    <param name="BufferingTime" value=0>';
			str += '    <param name="windowlessVideo" value="0">';
			str += '    <param name="fullScreen" value="0">';
			str += '    <param name="enableErrorDialogs" value="0">';
			str += '</object>';
			this.mdivScreen.innerHTML = str;
			Player =  document.getElementById("player");
			pver = Player.versionInfo.split('.')[0];
			this.player =  Player;
			this.player.URL = url;
			this.player.controls.play();
		}
		this.mdivPlay.style.backgroundPosition='0% -33px';


		widthslot = $("track").offsetWidth;
		widthskid = $("trackBar").offsetWidth;
		new Control.Slider('trackBar','track',{
			startSpan:'trackNow',
			sliderValue:0,
			onChange:function(v){
				v += widthskid/(2*widthslot);
				if(is_vista)
				{
					Player.controls.currentPosition = Player.currentMedia.duration * v;
				}else
				{
					var position = findIndex(Player.currentMedia.duration * v);
					if(position>0)
					{
						//		debug('set point ' + keyframe_num[position]*packagesize +' + '+ headersize);
						Player.URL = media_url + '?start=' + (keyframe_num[position]*packagesize-0+headersize);
						Player.controls.play();
						//fix media player 9
						if(pver<10)
						{
							basepoint = v*widthslot;
						}
						bufftime = keyframe_time[position];
					}
				}
			}
		});
		new Control.Slider('volumeBar','volumeTrack',{
			startSpan:'volumeNow',
			sliderValue: Player.settings.volume/100,
			onSlide:function(v){
				//	debug(v*100);
				Player.settings.volume = 100 * v;
			}
		});


		Event.observe($('trackNow'),'click', function(event)
		{
			point = Event.pointerX(event) - Position.cumulativeOffset($('plan'))[0];
			v = point/widthslot+widthskid/(2*widthslot);
			if(is_vista)
			{
				Player.controls.currentPosition = Player.currentMedia.duration * v;
				move(point);
			}else
			{
				var position = findIndex(Player.currentMedia.duration * v);
				if(position>0)
				{
					Player.URL = media_url + '?start=' + (keyframe_num[position]*packagesize-0+headersize);
					Player.controls.play();
					move(point);
					//fix media player 9
					if(pver<10)
					{
						basepoint = v*widthslot;
					}
					bufftime = keyframe_time[position];
				}
			}
		});
		Event.observe(window, 'load', function(){
			Timer = setInterval('container()', 1000);
		});

		try
		{
			this.m_time = setInterval('curPlayer.Monitor()',1000);
			this.timerRunning = true;
		}catch(err){}
		//全屏
		this.mdivFullScreen.onclick=function()
		{
			if(mPlayerVod.player.playState==3)
			{
				mPlayerVod.player.fullScreen = 1;
			}
		}
		//静音
		this.mdivSound.onclick=function()
		{
			if (mPlayerVod.player.settings.mute) {mPlayerVod.mdivSound.style.backgroundPosition='0% -68px';}else{mPlayerVod.mdivSound.style.backgroundPosition='0% -308px';}
			mPlayerVod.player.settings.mute=!mPlayerVod.player.settings.mute;
		}
		//播放
		this.mdivPlay.onclick=function()
		{
			if (!mPlayerVod.isplay)
			{
				try{
					mPlayerVod.ShowMovie(1);//关闭广告画面
					mPlayerVod.mdivPlay.style.backgroundPosition='0% -33px';
					mPlayerVod.player.controls.play();
					mPlayerVod.isplay=true;
				}catch(err){}
				return ;
			}
			if(mPlayerVod.mdivPlay.style.backgroundPosition=='0% -33px')
			{
				try{
					mPlayerVod.mdivPlay.style.backgroundPosition='0 0';
					mPlayerVod.player.controls.pause();
				}catch(err){}
			}
			else
			{
				try{
					mPlayerVod.mdivPlay.style.backgroundPosition='0% -33px';
					mPlayerVod.player.controls.play();
				}catch(err){}
			}
		}
		//停止
		this.mdivStop.onclick=function()
		{
			try{
				mPlayerVod.isplay = false;
				mPlayerVod.mdivPlay.style.backgroundPosition='0 0';
				mPlayerVod.player.controls.stop();
				mPlayerVod.ShowMovie(3);
			}catch(err){}
		}
		//this.SetVolume(curVolumeBar.GetPos());//设置初始声音
		this.mdivTime.innerHTML = "";//播放时间显示
		document.getElementById("ctrlPanel").style.display="block";
		document.getElementById("track").style.display="block";//进度条隐藏
		this.ShowMovie(false);//显示广告画面
	},
	//获取播放状态
	GetStatus:function()
	{
		return this.player.playState;
	},
	//获取总长度
	GetLen:function()
	{
		return curPlayer.player.CurrentMedia.duration*1000;
	},
	//当前播放长度
	GetPos:function()
	{
		return this.player.controls.CurrentPosition*1000;
	},
	//设置播放地址并开始播放
	SetInfo:function(info)
	{
		try{
			this.mdivInfo.innerHTML = info;
		}catch(err){}
	},
	//设置声音
	SetVolume:function(v)
	{
		try{
			this.player.settings.volume=parseInt(v,10);
			return true;
		}catch(err){return false;}
	},
	//是否显示节目画面，true 为显示节目画面，false为显示广告画面
	ShowMovie:function(show)
	{
		if(show==1)
		{
			if (this.mdivReady.style.display != "none")
			{
				this.mdivReady.style.display = "none";
				this.mdivScreen.style.display = "block";
			}
			if(this.mdivAd.style.display!="none")
			{
				this.mdivScreen.style.display = "block";
				this.mdivAd.style.display = "none";
			}
		}
		else if (show==2)
		{
			if (this.mdivReady.style.display != "none")
			{
				this.mdivReady.style.display = "none";
			}
			if(this.mdivAd.style.display != "block")
			{
				this.mdivScreen.style.display = "none";
				this.mdivAd.style.display = "block";
			}
		}
		else if (show==3)
		{
			this.mdivAd.style.display = "none";
			this.mdivScreen.style.display = "none";
			this.mdivReady.style.display = "block";
		}
	},
	Monitor:function()
	{
		var state = this.GetStatus();
		this.SetInfo(this.stateStr[state])
		if(this.isvod)
		{
			try
			{
				if(Player.playState==3)
				{
					this.ShowMovie(true);
					if(packagesize==-1)
					{
						totaltime = Player.currentMedia.duration;
						packagesize = 0;
						var new_script = document.createElement("script");
						new_script.src = media_url+'?end=1';
						$('script_container').appendChild(new_script);
					}
					dot = Math.floor((widthslot*Player.controls.currentPosition)/Player.currentMedia.duration);
					move(dot);
				}else if(Player.playState==1)
				{
					move(0);
				}
			}catch(err){}
		}
	}
};

function parseKeyframe(p0, p1, p2, p3)
{
	packagesize = p0;
	headersize = p1;
	keyframe_time = p2;
	keyframe_time.push(totaltime);
	keyframe_num = p3;
	acceTime(keyframe_time);
}
function acceTime(ptime)
{
	time_obj.middleIndex = Math.floor(ptime.length/2);
	time_obj.middleTime = ptime[time_obj.middleIndex];
	time_obj.firstIndex = Math.floor(time_obj.middleIndex/2);
	time_obj.firstTime = ptime[time_obj.firstIndex];
	time_obj.endIndex = time_obj.middleIndex + time_obj.firstIndex;
	time_obj.endTime = ptime[time_obj.endIndex];
}
function findIndex(ptime)
{
	if(ptime<time_obj.middleTime)
	{
		if(ptime<time_obj.firstTime)
		{
			start = 0;
			end = time_obj.firstIndex;
		}else
		{
			start = time_obj.firstIndex;
			end = time_obj.middleIndex;
		}
	}
	else
	{
		if(ptime<time_obj.endTime)
		{
			start = time_obj.middleIndex;
			end = time_obj.endIndex;
		}else
		{
			start = time_obj.endIndex;
			end = keyframe_time.length;
		}
	}
	for(i=start;i<end;i++)
	{
		if(keyframe_time[i]<ptime && keyframe_time[i+1]>ptime)
		{
			return i;
		}
	}
	return -1;
}
function move(point)
{
	if(point>widthslot){
		point = widthslot;
	}
	//fix meida player 9
	if(pver<10 && basepoint>0)
	{
		$('percent').innerHTML = formatTime(Player.controls.currentPosition + bufftime) + '/' + Player.currentMedia.durationString;
	}else
	{
		$('percent').innerHTML = (Player.controls.currentPositionString?Player.controls.currentPositionString:'00:00') + '/' + Player.currentMedia.durationString;
	}
	var temppos = (basepoint + point)/widthslot*100 + "%";
//	var temppos = (Player.controls.CurrentPosition?Player.controls.CurrentPosition:0)/Player.CurrentMedia.duration*100 + "%";
	$('trackBar').style.left = temppos;
	$('trackNow').style.width = temppos;
}
function container()
{
	//	debug('status: ' + Player.playState)
	if(Player.playState==3)
	{
		if(packagesize==-1)
		{
			totaltime = Player.currentMedia.duration;
			packagesize = 0;
			var new_script = document.createElement("script");
			new_script.src = media_url+'?end=1';
			$('script_container').appendChild(new_script);
		}
		dot = Math.floor((widthslot*Player.controls.currentPosition)/Player.currentMedia.duration);
		move(dot);
	}else if(Player.playState==1)
	{
		move(0);
	}
}
function formatTime(t)
{
	s = Math.floor(t%60);
	if(s<10)
	{
		s = '0'+s;
	}
	m = Math.floor((t%3600)/60);
	if(m<10)
	{
		m = '0' + m;
	}
	h = Math.floor(t/3600);
	if(h==0)
	{
		return m+':'+s;
	}
	if(h<10)
        {
                h = '0' + h;
        }
	return h+':'+m+':'+s;
}